<?php
declare(strict_types=1);
namespace app\shop\logic;
use app\admin\model\vote\Type;
use app\shop\exception\FailException;
use think\Collection;
use app\shop\model\vote\vote;

class VoteLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new vote();
    }

    public function VoteList($params): Collection|array
    {
        $query = vote::getQuery();
        $query->where([
            ['vote_type_id', '=', $params['vote_type_id']],
            ['switch', '=', 1],
        ]);
        $this->buildEqualQuery($query, $params, [
        ]);

        $query->field([
            '*'
        ]);
        $query->order(['num' => 'desc']);
        $return = vote::paginateListQuery($query,true);
        return $return;
    }

    public function typeList(): Collection|array
    {
        $query = \app\shop\model\vote\type::getQuery();
        $query->field(['*']);
        $query->order(['id' => 'desc']);
        return \app\shop\model\vote\type::listQuery($query);
    }

    public function detail(array $params): array
    {
        $id = $params['id'] ?? 0;

        $query = vote::where([
            ['id', '=', $id],
            ['switch', '=', 1]
        ]);
        $result = $query
            ->findOrEmpty();

        if ($result->isEmpty()) {
            throw new FailException('暂无数据');
        }
        return $result->toArray();
    }

}